﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using warcaby.Backpropagation;
using warcaby.LearningRateFunctions;
using warcaby.Initializers;
using warcaby.Properties;
using warcaby.SOM;
using System.Runtime.Serialization.Formatters.Binary;

namespace warcaby
{
    public partial class Plansza : Form
    {
        private Graphics grap;
        private Map map=new Map();
        int clickx = -1;
        int clicky = -1;
        LayeredLearning L = new LayeredLearning();

        string name = "noname";

        TDLearning TD = new TDLearning();

        public Plansza()
        {
            InitializeComponent(); 
        }
        private void Plansza_Load(object sender, EventArgs e)
        {
            grap = pictureBoxcheckers.CreateGraphics();
            map = new Map();
        }
        private void button_start_Click(object sender, EventArgs e)
        {
            map = new Map();
            OnPaint(null);
        }

        private void draw(int x, int y, type color)
        {
            int siz = pictureBoxcheckers.Width/8;
            x = x * siz;
            y = y * siz;
            Brush c=Brushes.BurlyWood;
            Rectangle rec = new Rectangle(new Point(x, y), new Size(siz,siz));
            if (color == type.EmptyW)
            {
                c = Brushes.Aquamarine;
                grap.FillRectangle(c, rec);
            }  
            if (color == type.Empty)
            {
                c = Brushes.BurlyWood;
                grap.FillRectangle(c,rec);
            }

            if (color == type.Black)
            {
                c = Brushes.BurlyWood;
                grap.FillRectangle(c, rec);
                c = Brushes.Black;
                grap.FillEllipse(c,rec);
            }

            if (color == type.White)
            {
                c = Brushes.BurlyWood;
                grap.FillRectangle(c, rec);
                c = Brushes.White;
                grap.FillEllipse(c, rec);
            }
            if (color == type.BlackQueen)
            {
                c = Brushes.BurlyWood;
                grap.FillRectangle(c, rec);
                c = Brushes.DarkGoldenrod;
                grap.FillEllipse(c, rec);
            }

            if (color == type.WhiteQueen)
            {
                c = Brushes.BurlyWood;
                grap.FillRectangle(c, rec);
                c = Brushes.Chartreuse;
                grap.FillEllipse(c, rec);
            }

        }


        protected override void OnPaint(PaintEventArgs paintEvnt)
        {
            grap = pictureBoxcheckers.CreateGraphics();
            for (int x = 0; x < 8; x++)
                for (int y = 0; y < 8; y++)
                    draw(x, y, map.GetArea(x, y));
            
        }



        private void button_next_Click(object sender, EventArgs e)
        {
            Stack<Move> Moves=new Stack<Move>();
            Moves = map.GetAllMoves();
            Move best = new Move();
            Move M1=new Move();
            int count = Moves.Count;
            int k = Moves.Count();
            Random rand = new Random();
            M1 = Moves.ToArray()[rand.Next()%k];

            
            if (map.Turn == "black" && count == 0)
            {
                MessageBox.Show("Congratualtions, the winner is white");
                return;
            }
            if (map.Turn == "white" && count == 0)
            {
                MessageBox.Show("Congratualtions, the winner is black");
                return;
            }
            if (map.Withoutslaughter >= 30)
            {
                MessageBox.Show("Congratualtions, you have a dRaW");
                return;
            }
                if (map.Move(M1) == -1)
                    MessageBox.Show("Error, impossible move: (" + M1.x1 + "," + M1.y1 + ") to" + "(" + M1.x2 + "," + M1.y2 + ")");


            labelturn.Text = "Turn: " + map.Turn;
            OnPaint(null);
        }

        private void pictureBoxcheckers_Click(object sender, EventArgs e)
        {
            int siz = pictureBoxcheckers.Width / 8;
            Stack<Move> Moves = new Stack<Move>();
            Moves = map.GetAllMoves();
            if (Moves.Count != 0)
            {

            }
            else if (map.Turn == "black")
                MessageBox.Show("Congratualtions, the winner is white");
            else if (map.Turn == "white")
                MessageBox.Show("Congratualtions, the winner is black");
            if (clickx == -1)
            {

                    
                clickx = pictureBoxcheckers.PointToClient(MousePosition).X/siz;
                clicky = pictureBoxcheckers.PointToClient(MousePosition).Y/siz;
                type t = map.GetArea(clickx, clicky); 
                if(t!=type.Black&&t!=type.BlackQueen&&t!=type.White&&t!=type.WhiteQueen)
                {
                    MessageBox.Show("Error, click "+map.Turn);
                    clickx = -1;
                    return;
                }
                labelfrom.Text="( "+clickx+","+clicky+" )";
                labelto.Text="click";
                return;
            }
            int xtmp = pictureBoxcheckers.PointToClient(MousePosition).X/siz;
            int ytmp = pictureBoxcheckers.PointToClient(MousePosition).Y/siz;
            Move move = new Move();
            move.x1 = clickx;
            move.x2 = xtmp;
            move.y1 = clicky;
            move.y2 = ytmp;
            if (map.Move(move) == -1)
                MessageBox.Show("Error, impossible move");
            labelfrom.Text="click";
            labelto.Text="( "+xtmp+","+ytmp+" )";

            OnPaint(null);
                           
            clickx = -1;
            clicky = -1;
            labelturn.Text = "Turn: "+map.Turn;
        }

        private void button_nextMinMax_Click(object sender, EventArgs e)
        {
            Stack<Move> Moves = new Stack<Move>();
            Moves = map.GetAllMoves();
            Stack<Move> stackbest = new Stack<Move>();
            Move best = new Move();
            int count = Moves.Count;


            if (map.Turn == "black" && count == 0)
            {
                MessageBox.Show("Congratualtions, the winner is white");
                return;
            }
            if (map.Turn == "white" && count == 0)
            {
                MessageBox.Show("Congratualtions, the winner is black");
                return;
            }
            if (map.Withoutslaughter == 30)
            {
                MessageBox.Show("Congratualtions, you have a dRaW");
                return;
            }


            stackbest = L.MinMaxAB(map, Moves, 7);
            Random rand = new Random();
            int happybest=rand.Next() % stackbest.Count;
            best = stackbest.ToArray()[happybest];
            if (map.Move(best) == -1)
                MessageBox.Show("Error, impossible move: (" + best.x1 + "," + best.y1 + ") to" + "(" + best.x2 + "," + best.y2 + ")");

            labelmovenr.Text = "Move nr: " + map.Movenr;
            labelturn.Text = "Turn: " + map.Turn;
            OnPaint(null);
        }
        private void button4_Click(object sender, EventArgs e)
        {
            // ?? - Nie wiem do czego to
            Stack<Move> stackbest = new Stack<Move>();
            Move best = new Move();
            int happybest;

            int nrofgames;
            int blacklevel;
            int whitelevel;
            if (!int.TryParse(textBox13.Text, out nrofgames))
                nrofgames = 10;
            if (!int.TryParse(textBox12.Text, out blacklevel))
                blacklevel = 3;
            if (!int.TryParse(textBox11.Text, out whitelevel))
                whitelevel = 3;


            int winnerblack = 0;
            int winnerwhite = 0;
            int equal = 0;
            int count;

            Stack<Move> Moves = new Stack<Move>();
            Random rand = new Random();
            while (winnerblack + winnerwhite + equal != nrofgames)
            {
                Moves = map.GetAllMoves();
                count = Moves.Count;
                if (map.Withoutslaughter >= 30)
                {
                    equal++;
                    map = new Map();
                    Console.Write(winnerblack + winnerwhite + equal);
                    label26.Text = (winnerblack + winnerwhite + equal).ToString();
                    this.Refresh();
                    continue;
                }
                if (map.Turn == "black" && count == 0)
                {
                    winnerwhite++;
                    map = new Map();
                    Console.Write(winnerblack + winnerwhite + equal);

                    label26.Text = (winnerblack + winnerwhite + equal).ToString();
                    this.Refresh();
                    continue;
                }
                if (map.Turn == "white" && count == 0)
                {
                    winnerblack++;
                    map = new Map();

                    Console.Write(winnerblack + winnerwhite + equal);

                    label26.Text = (winnerblack + winnerwhite + equal).ToString();
                    this.Refresh();
                    continue;
                }
                count = Moves.Count;

                if (map.Turn == "white")
                {

                    //Granie z 30% zbiorem walidacyjnym

                    // Granie z MinMaxem
                    stackbest = L.MinMaxAB(map, Moves, whitelevel);
                    happybest = rand.Next() % stackbest.Count;
                    best = stackbest.ToArray()[happybest];

                    //Granie ze sobą
                    //stackbest = L.MinMaxAB(map, Moves, blacklevel);
                    /*Move M1 = new Move();
                    double bestvalue = -1111;
                    while (Moves.Count != 0)
                    {
                        M1 = Moves.Pop();
                        Move M1_temp = zamiana_ruchu_x_na_y(M1);
                        double[] plansza_z_mozliwym_ruchem = przygotuj_plansze_z_ruchem(map.getArray(), M1_temp, map.Turn);
                        double[] val1 = TD.network.Run(plansza_z_mozliwym_ruchem); // !!!!!!!!!! DUŻY BŁĄD..
                        double val = val1[0];
                        if (map.Turn == "black")
                            if (bestvalue < val || bestvalue == -1111)
                            {
                                best = M1;
                                bestvalue = val;
                            }
                        if (map.Turn == "white")
                            if (bestvalue < val || bestvalue == -1111)
                            {
                                best = M1;
                                bestvalue = val;
                            }
                    }*/
                }
                if (map.Turn == "black")
                {
                    //stackbest = L.MinMaxAB(map, Moves, blacklevel);
                    Move M1 = new Move();
                    double bestvalue = -1111;
                    while (Moves.Count != 0)
                    {
                        M1 = Moves.Pop();
                        Move M1_temp = zamiana_ruchu_x_na_y(M1);
                        double[] plansza_z_mozliwym_ruchem = przygotuj_plansze_z_ruchem(map.getArray(), M1_temp, map.Turn);
                        double[] val1 = TD.network.Run(plansza_z_mozliwym_ruchem); // !!!!!!!!!! DUŻY BŁĄD..
                        double val = val1[0];
                        if (map.Turn == "black")
                            if (bestvalue < val || bestvalue == -1111)
                            {
                                best = M1;
                                bestvalue = val;
                            }
                        if (map.Turn == "white")
                            if (bestvalue < val || bestvalue == -1111)
                            {
                                best = M1;
                                bestvalue = val;
                            }
                    }
                }  
                if (map.Move(best) == -1)
                    MessageBox.Show("Error, impossible move: (" + best.x1 + "," + best.y1 + ") to" + "(" + best.x2 + "," + best.y2 + ")");

                //    Console.Write( "Move nr: " + map.Movenr+".");
                OnPaint(null);
            }
            Console.WriteLine("black:white:draw-  " + winnerblack + ":" + winnerwhite + ":" + equal);
            label26.Text = winnerblack.ToString() + ":" + winnerwhite.ToString() + ":" + equal.ToString();
        }
        private void button_test_Click(object sender, EventArgs e)
        {
            Stack<Move> Moves = new Stack<Move>();
            Stack<Move> stackbest = new Stack<Move>();
            int nrofgames;
            int blacklevel;
            int whitelevel;
            if (!int.TryParse(textBox1.Text,out nrofgames))
                nrofgames = 10;
            if (!int.TryParse(textBox2.Text, out blacklevel))
                blacklevel = 4;
            if (!int.TryParse(textBox3.Text, out whitelevel))
                whitelevel = 4;
            if (nrofgames < 1)
                nrofgames = 1;
            if (blacklevel < 1)
                blacklevel = 1;
            if (whitelevel < 1)
                whitelevel = 1;
            Move best = new Move();
            int count;
            Random rand = new Random();
            int happybest;
            int winnerblack = 0;
            int winnerwhite = 0;
            int equal = 0;
            while (winnerblack+winnerwhite+equal != nrofgames)
            {
                
                Moves = map.GetAllMoves();
                count = Moves.Count;
                if (map.Withoutslaughter >= 20)
                {
                    equal++;
                    map = new Map();
                    Console.Write(winnerblack + winnerwhite + equal);
                    label23.Text = (winnerblack + winnerwhite + equal).ToString() ;
                    this.Refresh();
                    continue;
                }
                if (map.Turn == "black" && count == 0)
                {
                    winnerwhite++;
                    map = new Map();
                    Console.Write(winnerblack + winnerwhite+equal);

                    label23.Text = (winnerblack + winnerwhite + equal).ToString();
                    this.Refresh();
                    continue;
                }
                if (map.Turn == "white" && count == 0)
                {
                    winnerblack++;
                    map = new Map();

                    Console.Write(winnerblack + winnerwhite+equal);

                    label23.Text = (winnerblack + winnerwhite + equal).ToString();
                    this.Refresh();
                    continue;
                }
                count = Moves.Count;

                if(map.Turn=="white")
                     stackbest = L.MinMaxAB(map, Moves, whitelevel);
                if(map.Turn=="black")
                    stackbest = L.MinMaxAB(map, Moves, blacklevel);
                happybest = rand.Next() % stackbest.Count;
                best = stackbest.ToArray()[happybest];
                if (map.Move(best) == -1)
                    MessageBox.Show("Error, impossible move: (" + best.x1 + "," + best.y1 + ") to" + "(" + best.x2 + "," + best.y2 + ")");

            //    Console.Write( "Move nr: " + map.Movenr+".");
                OnPaint(null);
            }
            Console.WriteLine("black:white:draw-  " + winnerblack + ":" + winnerwhite+":"+equal);
            label8.Text = winnerblack.ToString() + ":" + winnerwhite.ToString() + ":" + equal.ToString();
        }

        private void buttonLLmove_Click(object sender, EventArgs e)
        {
            Stack<Move> Moves = new Stack<Move>();
            Moves = map.GetAllMoves();
            Move best = new Move();
            double bestvalue = -1111;
            Move M1 = new Move();
            int count = Moves.Count;
            while (Moves.Count != 0)
            {
                M1 = Moves.Pop();
                double[] val1 = L.network.Run(map.getArray());
                double val = val1[0];
                if (map.Turn == "black")
                    if (bestvalue < val || bestvalue == -1111)
                    {
                        best = M1;
                        bestvalue = val;
                    }
                if (map.Turn == "white")
                    if (bestvalue > val || bestvalue == -1111)
                    {
                        best = M1;
                        bestvalue = val;
                    }

            }
            if (map.Turn == "black" && count == 0)
            {
                MessageBox.Show("Congratualtions, the winner is white");
                return;
            }
            if (map.Turn == "white" && count == 0)
            {
                MessageBox.Show("Congratualtions, the winner is black");
                return;
            }
            if (map.Move(M1) == -1)
                MessageBox.Show("Error, impossible move: (" + M1.x1 + "," + M1.y1 + ") to" + "(" + M1.x2 + "," + M1.y2 + ")");

            labelturn.Text = "Turn: " + map.Turn;
            OnPaint(null);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Stream stream = File.Open("test.txt", FileMode.Create);
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(stream, L.network);
            stream.Close();



        }

        private void button3_Click(object sender, EventArgs e)
        {
            int neurallevel;
            if (!int.TryParse(textBox4.Text, out neurallevel))
                neurallevel = 1;
            L.Training(neurallevel);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Stack<Move> Moves = new Stack<Move>();
            LayeredLearning L = new LayeredLearning();
            Stack<Move> stackbest = new Stack<Move>();
            int nrofgames;
            int blacklevel;
            int whitelevel;
            if (!int.TryParse(textBox6.Text, out nrofgames))
                nrofgames = 10;
            if (!int.TryParse(textBox5.Text, out blacklevel))
                blacklevel = 4;
            if (nrofgames < 1)
                nrofgames = 1;
            Move best = new Move();
            int count;
            Random rand = new Random();
            int happybest;
            int winnerblack = 0;
            int winnerwhite = 0;
            int equal = 0;
            while (winnerblack + winnerwhite + equal != nrofgames)
            {

                Moves = map.GetAllMoves();
                count = Moves.Count;
                if (map.Withoutslaughter >= 20)
                {
                    equal++;
                    map = new Map();
                    Console.Write(winnerblack + winnerwhite + equal);
                    label23.Text = (winnerblack + winnerwhite + equal).ToString();
                    this.Refresh();
                    continue;
                }
                if (map.Turn == "black" && count == 0)
                {
                    winnerwhite++;
                    map = new Map();
                    Console.Write(winnerblack + winnerwhite + equal);

                    label23.Text = (winnerblack + winnerwhite + equal).ToString();
                    this.Refresh();
                    continue;
                }
                if (map.Turn == "white" && count == 0)
                {
                    winnerblack++;
                    map = new Map();

                    Console.Write(winnerblack + winnerwhite + equal);

                    label23.Text = (winnerblack + winnerwhite + equal).ToString();
                    this.Refresh();
                    continue;
                }
                count = Moves.Count;

                if (map.Turn == "white")
                     stackbest = L.LLmove(map, Moves);
                if (map.Turn == "black")
                    stackbest = L.MinMaxAB(map, Moves, blacklevel);
                   
                
                happybest = rand.Next() % stackbest.Count;
                best = stackbest.ToArray()[happybest];
                if (map.Move(best) == -1)
                    MessageBox.Show("Error, impossible move: (" + best.x1 + "," + best.y1 + ") to" + "(" + best.x2 + "," + best.y2 + ")");

                //    Console.Write( "Move nr: " + map.Movenr+".");
                OnPaint(null);
            }
            Console.WriteLine("black:white:draw-  " + winnerblack + ":" + winnerwhite + ":" + equal);
            label9.Text = winnerblack.ToString() + ":" + winnerwhite.ToString() + ":" + equal.ToString();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Stream stream = File.Open("test.txt", FileMode.Open);
            BinaryFormatter bf = new BinaryFormatter();
            L.network=(BackpropagationNetwork)bf.Deserialize(stream);
            stream.Close();
        }

        private void button9_Click(object sender, EventArgs e)
        {
           
            L = new LayeredLearning();
            name = textBox10.Text;

        }
        private double[] przygotuj_plansze_z_ruchem(double[] p, Move M1, string gracz)//p - plansza
        {
            double[] ret_p = new double[34];
            Array.Copy(p,ret_p,34);

            //int i = (((4+1)*4)-1)%2;
            int i1 = Convert.ToInt32((M1.y1 * 4) + Math.Floor(Convert.ToDouble(M1.x1/2)));
            int i2 = Convert.ToInt32((M1.y2 * 4) + Math.Floor(Convert.ToDouble(M1.x2/2)));

            if (Math.Abs(M1.x1 - M1.x2) > 1 || Math.Abs(M1.y1 - M1.y2) > 1) // typ_ruchu == 'x' UWAGA ! Pytanie do siebie: Czy siec poprawnie wyznaczy że 2 bicia > 1 bicia ? -> raczej tak
            {
                double temp = ret_p[i1];
                ret_p[i1] = ret_p[i2];
                ret_p[i2] = temp;

                // wymazanie bitego pionka:
                //Math.Abs()
                if (M1.x2 - M1.x1 < 0 && M1.y2 - M1.y1 < 0) // bicie w dolny lewy róg 
                {
                    ret_p[Convert.ToInt32((checkIfBelowZeroThenEqualZero(M1.y1 - 1) * 4) + Math.Floor(Convert.ToDouble(checkIfBelowZeroThenEqualZero(M1.x1 - 1) / 2)))] = 0;
                }
                else if (M1.x2 - M1.x1 < 0 && M1.y2 - M1.y1 > 0)// bicie w górny lewy róg 
                {
                    ret_p[Convert.ToInt32((checkIfBelowZeroThenEqualZero(M1.y1 - 1) * 4) + Math.Floor(Convert.ToDouble((M1.x1 + 1) / 2)))] = 0;
                }
                else if (M1.x2 - M1.x1 > 0 && M1.y2 - M1.y1 < 0)// bicie w dolny prawy róg 
                {
                    ret_p[Convert.ToInt32((checkIfBelowZeroThenEqualZero(M1.y1 - 1) * 4) + Math.Floor(Convert.ToDouble(checkIfBelowZeroThenEqualZero(M1.x1 + 1) / 2)))] = 0;
                }
                else if (M1.x2 - M1.x1 > 0 && M1.y2 - M1.y1 > 0)// bicie w górny prawy róg 
                {
                    ret_p[Convert.ToInt32(((M1.y1 + 1) * 4) + Math.Floor(Convert.ToDouble((M1.x1 + 1) / 2)))] = 0;
                }

                //return convertRawInputToInput(input);

            }
            else 
            {
                
                double temp = ret_p[i1];
                ret_p[i1] = ret_p[i2];
                ret_p[i2] = temp;
                //return convertRawInputToInput(input);  
            }
            return ret_p;//throw new NotImplementedException();
        }

        private int checkIfBelowZeroThenEqualZero(int p)
        {
            if(p < 0)
            {
                return 0;
            }
            return p;
        }
        private Move zamiana_ruchu_x_na_y(Move M1)
        {
            return new Move(M1.y1, M1.x1, M1.y2, M1.x2);
        }
        private void button10_Click(object sender, EventArgs e)
        {
              Stack<Move> Moves = new Stack<Move>();
            Moves = map.GetAllMoves();
            Move best = new Move();
            double bestvalue = -1111;
            Move M1 = new Move();
            int count = Moves.Count;
            while (Moves.Count != 0)
            {
                M1 = Moves.Pop();
                Move M1_temp = zamiana_ruchu_x_na_y(M1);
                double[] plansza_z_mozliwym_ruchem = przygotuj_plansze_z_ruchem(map.getArray(), M1_temp, map.Turn);
                double[] val1 = TD.network.Run(plansza_z_mozliwym_ruchem); // !!!!!!!!!! DUŻY BŁĄD..
                double val = val1[0];
                if (map.Turn == "black")
                    if (bestvalue < val || bestvalue == -1111)
                    {
                        best = M1;
                        bestvalue = val;
                    }
                if (map.Turn == "white")
                    if (bestvalue < val || bestvalue == -1111)
                    {
                        best = M1;
                        bestvalue = val;
                    }
            }

            if (map.Turn == "black" && count == 0)
            {
                MessageBox.Show("Congratualtions, the winner is white");
                return;
            }
            if (map.Turn == "white" && count == 0)
            {
                MessageBox.Show("Congratualtions, the winner is black");
                return;
            }
            if (map.Move(best) == -1)
                MessageBox.Show("Error, impossible move: (" + M1.x1 + "," + M1.y1 + ") to" + "(" + M1.x2 + "," + M1.y2 + ")");

            labelturn.Text = "Turn: " + map.Turn;
            OnPaint(null);
        }

        

        private void button2_Click(object sender, EventArgs e)
        {
            Stack<Move> Moves = new Stack<Move>();
            LayeredLearning L = new LayeredLearning();
            Stack<Move> stackbest = new Stack<Move>();
            int nrofgames;
            int blacklevel;
            int whitelevel;
            if (!int.TryParse(textBox6.Text, out nrofgames))
                nrofgames = 10;
            if (!int.TryParse(textBox5.Text, out blacklevel))
                blacklevel = 4;
            if (nrofgames < 1)
                nrofgames = 1;
            Move best = new Move();
            int count;
            Random rand = new Random();
            int happybest;
            int winnerblack = 0;
            int winnerwhite = 0;
            int equal = 0;
            while (winnerblack + winnerwhite + equal != nrofgames)
            {

                Moves = map.GetAllMoves();
                count = Moves.Count;
                if (map.Withoutslaughter >= 20)
                {
                    equal++;
                    map = new Map();
                    Console.Write(winnerblack + winnerwhite + equal);
                    label23.Text = (winnerblack + winnerwhite + equal).ToString();
                    this.Refresh();
                    continue;
                }
                if (map.Turn == "black" && count == 0)
                {
                    winnerwhite++;
                    map = new Map();
                    Console.Write(winnerblack + winnerwhite + equal);

                    label23.Text = (winnerblack + winnerwhite + equal).ToString();
                    this.Refresh();
                    continue;
                }
                if (map.Turn == "white" && count == 0)
                {
                    winnerblack++;
                    map = new Map();

                    Console.Write(winnerblack + winnerwhite + equal);

                    label23.Text = (winnerblack + winnerwhite + equal).ToString();
                    this.Refresh();
                    continue;
                }
                count = Moves.Count;

                if (map.Turn == "white")
                    stackbest = L.LLmove(map, Moves);
                if (map.Turn == "black")
                    stackbest = TD.TDmove(map,Moves);


                happybest = rand.Next() % stackbest.Count;
                best = stackbest.ToArray()[happybest];
                if (map.Move(best) == -1)
                    MessageBox.Show("Error, impossible move: (" + best.x1 + "," + best.y1 + ") to" + "(" + best.x2 + "," + best.y2 + ")");

                //    Console.Write( "Move nr: " + map.Movenr+".");
                OnPaint(null);
            }
            Console.WriteLine("black:white:draw-  " + winnerblack + ":" + winnerwhite + ":" + equal);
            label15.Text = winnerblack.ToString() + ":" + winnerwhite.ToString() + ":" + equal.ToString();
        }

        private void lavvlvla_Click(object sender, EventArgs e)
        {
            int epchos = Convert.ToInt32(textBox16.Text.ToString());
            int rank = Convert.ToInt32(textBox15.Text.ToString());
            int no_of_games = Convert.ToInt32(textBox14.Text.ToString());
            string mse = TD.Training(rank,no_of_games,epchos);
            MessageBox.Show("Done!");
        }

        
    }
}
